
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/common/constant/api.dart';
import 'package:guochaoapp/common/utils/http_client.dart';
import 'package:guochaoapp/model/invoice_entity.dart';
import 'package:guochaoapp/routes/app_pages.dart';
import 'package:guochaoapp/services/user_service.dart';

class GCInvoiceController extends GetxController {
  UserService userService = Get.find();

  var invoiceData = Rx<InvoiceData>();

  var titles = [
    "单位名称",
    "纳税人识别码",
    "注册地址",
    "注册电话",
    "开户银行",
    "银行账号",
    "收票姓名",
    "收票电话",
    "收票地址"
  ];

  submitTap() {
    if(invoiceData.value == null) {
      Get.toNamed(Routes.ADDINVOICE, arguments: ["new"]);
    } else {
      HttpClient.request(
          url: UserInvoiceApi.usersInvoice,
          method: Method.DELETE,
          data: {
            "uid" : userService.userInfo.uid
          },
          onSuccess: (r, _) {
            invoiceData.value = null;
          },
          onFailed: (err) {
            EasyLoading.showError(err);
          }
      );
    }
  }

  editTap() {
    Get.toNamed(Routes.ADDINVOICE, arguments: ["edit", invoiceData.value]);
  }

  requestInvoiceInfo() {

    HttpClient.request<InvoiceEntity>(
        url: UserInvoiceApi.usersInvoice,
        method: Method.GET,
        params: {
        "uid" : userService.userInfo.uid
        },
      onSuccess: (_, model) {
          invoiceData.value = model.data;
      },
      onFailed: (err) {
        EasyLoading.showError(err);
      }
    );

  }

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    requestInvoiceInfo();
  }
}